# -*- coding: utf-8 -*-
"""
@author: wyb
@date: 2023/12/18
@time: 20:30
@desc: Description
"""
import requests
from bs4 import BeautifulSoup


def save_html(content, filename):
    with open(filename, 'w', encoding='utf-8') as file:
        file.write(content)


def baidu_search(query):
    url = f'https://www.baidu.com/s?wd={query}'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
    }

    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response.text
    else:
        print('Error:', response.status_code)
        return None


# 搜索关键词
keyword = 'togaf'

# 执行搜索并保存结果
html_content = baidu_search(keyword)
if html_content:
    save_html(html_content, 'baidu_search.html')
    print('搜索结果已保存为 baidu_search.html 文件')
else:
    print('无法获取搜索结果')